package com.haocang.szgm.ui.waterpoint.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.haocang.szgm.R;
import com.haocang.szgm.activity.bean.FileIndexBean;
import com.haocang.szgm.activity.comm.CommMediaActivity;
import com.haocang.szgm.activity.tool.CacheActivityUtil;
import com.haocang.szgm.requestcenter.DataModel;
import com.haocang.commonlib.network.request.UploadUtil;
import com.haocang.commonlib.network.interfaces.InitDataOneBeanBack;
import com.haocang.szgm.ui.tools.activity.EditMemoActivity;
import com.haocang.szgm.ui.tools.activity.EnclosureListActivity;
import com.haocang.szgm.ui.tools.activity.MultiSelectDialog;
import com.haocang.szgm.ui.tools.enums.MultiSelectEnum;
import com.haocang.szgm.ui.waterpoint.bean.WaterPointBean;
import com.haocang.szgm.ui.waterpoint.utils.ComputeTools;
import com.haocang.szgm.utils.bean.ProvinceBean;
import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.config.WebApi;
import com.haocang.commonlib.loadingdialog.LoadingDialog;
import com.haocang.szgm.utils.tools.CacheData;
import com.haocang.szgm.utils.tools.CustomDialog;
import com.haocang.szgm.utils.tools.GetCommonParameters;
import com.haocang.szgm.utils.tools.MyTextUtils;
import com.haocang.szgm.utils.tools.OptionsItemsUtils;
import net.tsz.afinal.annotation.view.ViewInject;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class WaterPointDetailActivity extends CommMediaActivity {

    @ViewInject(id = R.id.ll_more_process)
    LinearLayout ll_more_process;
    @ViewInject(id = R.id.tv_SeepeNo)
    EditText tv_SeepeNo;
    @ViewInject(id = R.id.tv_Location)
    EditText tv_Location;
    @ViewInject(id = R.id.tv_SeeperArea)
    TextView tv_SeeperArea;
    @ViewInject(id = R.id.tv_SeeperDepth)
    TextView tv_SeeperDepth;
    @ViewInject(id = R.id.sb_SeeperDepth)
    SeekBar sb_SeeperDepth;
    @ViewInject(id = R.id.sb_SeeperArea)
    SeekBar sb_SeeperArea;
    @ViewInject(id = R.id.tv_StartWaterDate)
    TextView tv_StartWaterDate;
    @ViewInject(id = R.id.tv_SeeperSource)
    EditText tv_SeeperSource;
    @ViewInject(id = R.id.tv_SeeperReason)
    TextView tv_SeeperReason;
    @ViewInject(id = R.id.tv_Memo)
    TextView tv_Memo;
    @ViewInject(id = R.id.tv_SeeperLevel)
    TextView tv_SeeperLevel;
    @ViewInject(id = R.id.tv_WarningID)
    TextView tv_WarningID;
    @ViewInject(id = R.id.tv_DisposingPerson)
    TextView tv_DisposingPerson;
    @ViewInject(id = R.id.tv_EndWaterDate)
    TextView tv_EndWaterDate;
    @ViewInject(id = R.id.tv_WaterResult)
    TextView tv_WaterResult;
    @ViewInject(id = R.id.tv_ArrangementID)
    TextView tv_ArrangementID;

    @ViewInject(id = R.id.submit_yuchuli_lin)
    LinearLayout submit_yuchuli_lin;
    @ViewInject(id = R.id.water_chuzhi_tv_notask)
    Button water_chuzhi_tv_notask;
    @ViewInject(id = R.id.cb_IsRegression)
    CheckBox cb_IsRegression;
    @ViewInject(id = R.id.cb_IsHandle)
    CheckBox cb_IsHandle;
    @ViewInject(id = R.id.tv_Measures)
    TextView tv_Measures;
    @ViewInject(id = R.id.measures_lin)
    LinearLayout measures_lin;
    @ViewInject(id = R.id.submit_yuchuli)
    Button submit_yuchuli;
    @ViewInject(id = R.id.ck_1)
    CheckBox ck_1;
    @ViewInject(id = R.id.ck_2)
    CheckBox ck_2;
    @ViewInject(id = R.id.ck_3)
    CheckBox ck_3;
    @ViewInject(id = R.id.ck_4)
    CheckBox ck_4;
    @ViewInject(id = R.id.ck_status_0)
    CheckBox ck_status_0;
    @ViewInject(id = R.id.ck_status_1)
    CheckBox ck_status_1;
    @ViewInject(id = R.id.ck_status_2)
    CheckBox ck_status_2;
    @ViewInject(id = R.id.submit)
    Button submit;

    private Context context;
    protected double SingleTap_X = 0, SingleTap_Y = 0;
    private LoadingDialog dialog;
    private WaterPointBean model;
    private RequestQueue mQueue;
    private String ls_ArrangementID = "", ls_DisposingPersonID = "", ls_DisposingPersonName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CacheActivityUtil.addActivity(WaterPointDetailActivity.this);
        context = WaterPointDetailActivity.this;
        initView();

        initEvent();

        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CacheActivityUtil.finishSingleActivity(WaterPointDetailActivity.this);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_water_point_detail;
    }

    @Override
    protected boolean showNavigation() {
        return true;
    }

    @Override
    protected boolean showMedia() {
        return true;
    }

    @Override
    protected int showMoreIcon() {
        return R.drawable.icon_fujian_01;
    }

    @Override
    protected String showTitleName() {
        return "积水点";
    }

    private void initView() {
        mQueue = Volley.newRequestQueue(this);
        ll_more_process.setVisibility(View.VISIBLE);
        tv_SeeperSource.setEnabled(false);
    }

    private void initEvent() {

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, EnclosureListActivity.class);
                intent.putExtra("BILLID", model.getID());
                startActivity(intent);
            }
        });

        ll_more_process.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, WaterFlowActivity.class);
                intent.putExtra("ID", model.getID());
                startActivity(intent);
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSubmit();
            }
        });
        submit_yuchuli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                CustomDialog.Builder builder = new CustomDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("您确定对该积水点进行预处理?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                        MeasuresSubmit();
                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        water_chuzhi_tv_notask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "reportmemo");
                it.putExtra("DECIDE", "Name");
                it.putExtra("TYPE", MultiSelectEnum.NotaskMemo);
                it.putExtra("SELECTED", "");
                it.putExtra("IsAlone", false);
                it.putExtra("ManuaInput", true);
                startActivityForResult(it, 106);

               // Intent it = new Intent(WaterPointDetailActivity.this, EditMemoActivity.class);
               // startActivityForResult(it, 112);


            }
        });

        tv_StartWaterDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseTime(1);
            }
        });
        tv_DisposingPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", model.getArrangementID());
                it.putExtra("DECIDE", "ID");
                it.putExtra("TYPE", MultiSelectEnum.USER);
                it.putExtra("SELECTED", ls_DisposingPersonID);
                it.putExtra("IsAlone", false);
                startActivityForResult(it, 101);
            }
        });


        tv_Memo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "");
                it.putExtra("DECIDE", "Name");
                it.putExtra("TYPE", MultiSelectEnum.WaterMemo);
                it.putExtra("SELECTED", tv_Memo.getText().toString());
                it.putExtra("IsAlone", false);
                it.putExtra("ManuaInput", true);
                startActivityForResult(it, 105);
            }
        });


        tv_WaterResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "");
                it.putExtra("DECIDE", "Name");
                it.putExtra("TYPE", MultiSelectEnum.WaterResult);
                it.putExtra("SELECTED", tv_WaterResult.getText().toString());
                it.putExtra("IsAlone", false);
                it.putExtra("ManuaInput", true);
                startActivityForResult(it, 102);
            }
        });

        tv_Measures.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "");
                it.putExtra("DECIDE", "Name");
                it.putExtra("TYPE", MultiSelectEnum.Measures);
                it.putExtra("SELECTED", tv_Measures.getText().toString());
                it.putExtra("IsAlone", false);
                it.putExtra("ManuaInput", true);
                startActivityForResult(it, 104);
            }
        });


        tv_SeeperReason.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "");
                it.putExtra("DECIDE", "Name");
                it.putExtra("TYPE", MultiSelectEnum.SeeperReason);
                it.putExtra("SELECTED", tv_SeeperReason.getText().toString());
                it.putExtra("IsAlone", false);
                it.putExtra("ManuaInput", true);
                startActivityForResult(it, 103);
            }
        });
        tv_EndWaterDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseTime(2);
            }
        });
        tv_WarningID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseDeviceCode(OptionsItemsUtils.GetReasonList(), 6);
            }
        });

        tv_ArrangementID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(WaterPointDetailActivity.this, MultiSelectDialog.class);
                it.putExtra("ID", "");
                it.putExtra("DECIDE", "ID");
                it.putExtra("TYPE", MultiSelectEnum.Arrangement);
                it.putExtra("SELECTED", ls_ArrangementID);
                it.putExtra("IsAlone", true);
                startActivityForResult(it, 1);
            }
        });


        cb_IsHandle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tv_Measures.setText("设置围挡,打开雨水篦子");
                } else {
                    tv_Measures.setText("");
                }
            }
        });


        ck_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetFileStatus("3");
            }
        });
        ck_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SetFileStatus("0");
            }
        });
        ck_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetFileStatus("1");
            }
        });
        ck_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetFileStatus("2");
            }
        });


        ck_status_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(model.getStatus().equals("1")){
                    ToastShow("积水点已在处置中！");
                    SetBillStatus(1);
                    SetFileStatus("1");
                    return;
                }

                if(model.getStatus().equals("2")){
                    ToastShow("积水点已处置完成！");
                    SetBillStatus(2);
                    SetFileStatus("2");
                    return;
                }
                SetBillStatus(0);
                SetFileStatus("0");
                tv_EndWaterDate.setText("");

            }
        });
        ck_status_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(model.getStatus().equals("2")){
                    ToastShow("积水点已处置完成！");
                    SetBillStatus(2);
                    SetFileStatus("2");
                    return;
                }
                SetBillStatus(1);
                SetFileStatus("1");
                tv_EndWaterDate.setText("");
            }
        });
        ck_status_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                SetBillStatus(2);
                SetFileStatus("2");
                tv_EndWaterDate.setText(GetCommonParameters.getNowDatatoString());
                if (tv_WaterResult.getText().toString().trim().equals("")) {
                    tv_WaterResult.setText("清疏雨水箅加快排水");
                }
            }
        });

        sb_SeeperDepth.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_SeeperDepth.setText(Integer.toString(progress));
                ComputeTools.ComputeWaterLevel(tv_SeeperLevel,sb_SeeperDepth.getProgress(),sb_SeeperArea.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.e("------------", "开始滑动！");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.e("------------", "停止滑动！");
                int max = sb_SeeperDepth.getMax();
                if( seekBar.getProgress() > (max*9/10)){
                    sb_SeeperDepth.setMax(max * 2);
                }
                if( seekBar.getProgress() < (max/10)){
                    sb_SeeperDepth.setMax(max/2);
                }
            }
        });

        sb_SeeperArea.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_SeeperArea.setText(Integer.toString(progress));
                ComputeTools.ComputeWaterLevel(tv_SeeperLevel,sb_SeeperDepth.getProgress(),sb_SeeperArea.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.e("------------", "开始滑动！");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.e("------------", "停止滑动！");
                int max = sb_SeeperArea.getMax();
                if( seekBar.getProgress() > (max*9/10)){
                    sb_SeeperArea.setMax(max * 2);
                }
                if( seekBar.getProgress() < (max/10)){
                    sb_SeeperArea.setMax(max/2);
                }
            }
        });

    }

    private void initData() {

        if (!CacheData.WaterPoint_UPDATE) {
            Intent it = getIntent();
            SingleTap_X = it.getDoubleExtra("X", 0);
            SingleTap_Y = it.getDoubleExtra("Y", 0);
            submit.setVisibility(View.VISIBLE);
            iv_more.setVisibility(View.GONE);

            //判断当前是否存在预警
            if (!CacheData.Cache_WaringID.equals("")) {
                tv_WarningID.setText(MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetReasonList(), 1, CacheData.Cache_WaringID));
            }

        } else {
            initPageData();
        }

    }


    /**
     * ===============================================
     * 创建时间：2019/3/28 17:04
     * 编 写 人：ShenC
     * 方法说明：加载页面数据
     * ================================================
     * 修改内容：      修改时间：      修改人：
     * ===============================================
     *///加载页面数据
    private void initPageData() {
        dialog = new LoadingDialog(WaterPointDetailActivity.this, "加载中");
        dialog.show();
        String ApiUrl = Config.host
                + WebApi.Api_WaterPointDetail
                + CacheData.mWaterPointID;

        DataModel.GetApiRequestOneMode("WaterPointBean", ApiUrl, new InitDataOneBeanBack<WaterPointBean>() {

            @Override
            public void callbak(boolean b, WaterPointBean data, String msg) {

                if (b) {
                    model = data;

                    //当积水开始时间为空时
                    if (MyTextUtils.IsNullByString(model.getStartWaterDate())) {

                        if (model.getIsHandle() == 1 || model.getIsDelete().equals("Y")) {
                            measures_lin.setVisibility(View.GONE);
                            submit_yuchuli_lin.setVisibility(View.GONE);//预处理按钮模块隐藏
                        } else {
                            measures_lin.setVisibility(View.VISIBLE);
                            submit_yuchuli_lin.setVisibility(View.VISIBLE);//预处理按钮模块显示
                        }
                        //submit.setVisibility(View.GONE);

                    } else {
                        measures_lin.setVisibility(View.GONE);
                        submit_yuchuli_lin.setVisibility(View.GONE);//预处理按钮模块隐藏
                        submit.setVisibility(View.VISIBLE);

                        if (model.getStatus().equals("2")) {
                            submit.setVisibility(View.GONE);
                            water_chuzhi_tv_notask.setVisibility(View.GONE);
                        }

                    }


                    iv_more.setVisibility(View.VISIBLE);
                    SingleTap_X = model.getX();
                    SingleTap_Y = model.getY();
                    tv_SeepeNo.setText(model.getSeepeNo());
                    tv_Location.setText(model.getLocation());
                    if (model.getSeeperDepth() != null && !model.getSeeperDepth().equals("")) {
                        sb_SeeperDepth.setProgress(Integer.parseInt(model.getSeeperDepth()));
                        tv_SeeperDepth.setText(model.getSeeperDepth());
                    } else {
                        sb_SeeperDepth.setProgress(0);
                        tv_SeeperDepth.setText("0");
                    }
                    if (model.getSeeperArea() != null && !model.getSeeperArea().equals("")) {
                        sb_SeeperArea.setProgress(Integer.parseInt(model.getSeeperArea()));
                        tv_SeeperArea.setText(model.getSeeperArea());
                    } else {
                        sb_SeeperArea.setProgress(0);
                        tv_SeeperArea.setText("0");
                    }
                    tv_StartWaterDate.setText(model.getStartWaterDate());
                    tv_SeeperSource.setText(model.getSeeperSource());
                    tv_SeeperLevel.setText(MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetWaterPointSeeperLevel(), 1, model.getSeeperLevel()));
                    tv_SeeperReason.setText(model.getSeeperReason());
                    tv_Memo.setText(model.getMemo());
                    tv_WarningID.setText(MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetReasonList(), 1, model.getWarningID()));
                    tv_DisposingPerson.setText(model.getDisposingPersonName());
                    tv_EndWaterDate.setText(model.getEndWaterDate());

                    SetBillStatus(Integer.parseInt(model.getStatus()));
                    tv_WaterResult.setText(model.getWaterResult());
                    cb_IsRegression.setChecked(model.getIsRegression() == 0 ? false : true);
                    cb_IsHandle.setChecked(model.getIsHandle() == 0 ? false : true);
                    tv_Measures.setText(model.getMeasures());
                    ls_ArrangementID = model.getArrangementID() == null ? "" : model.getArrangementID();
                    ls_DisposingPersonID = model.getDisposingPerson() == null ? "" : model.getDisposingPerson();
                    tv_ArrangementID.setText(MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetArrangementBaseInfoList(), 1, model.getArrangementID()));
                    SetFileStatus(model.getStatus() == null ? "" :model.getStatus());
                } else {
                    ToastShow("数据加载出错：" + msg);
                    finish();
                }
                dialog.close();

            }
        });

    }


    //获取附件的状态
    private String GetFileStatus() {
        if (ck_1.isChecked()) {
            return "3";
        } else if (ck_2.isChecked()) {
            return "0";
        } else if (ck_3.isChecked()) {
            return "1";
        } else if (ck_4.isChecked()) {
            return "2";
        }
        return "0";
    }

    //设置附件的状态
    private void SetFileStatus(String Status) {
        switch (Status) {
            case "3":
                ck_1.setChecked(true);
                ck_2.setChecked(false);
                ck_3.setChecked(false);
                ck_4.setChecked(false);
                break;
            case "0":
                ck_1.setChecked(false);
                ck_2.setChecked(true);
                ck_3.setChecked(false);
                ck_4.setChecked(false);
                break;
            case "1":
                ck_1.setChecked(false);
                ck_2.setChecked(false);
                ck_3.setChecked(true);
                ck_4.setChecked(false);
                break;
            case "2":
                ck_1.setChecked(false);
                ck_2.setChecked(false);
                ck_3.setChecked(false);
                ck_4.setChecked(true);
                break;
        }
    }

    //提交工单
    private void initSubmit() {

        String SeepeNo = tv_SeepeNo.getText().toString();
        String Location = tv_Location.getText().toString();
        String SeeperDepth = tv_SeeperDepth.getText().toString();
        String SeeperArea = tv_SeeperArea.getText().toString();
        String SeeperReason = tv_SeeperReason.getText().toString();
        String Memo = tv_Memo.getText().toString().trim();
        String StartWaterDate = tv_StartWaterDate.getText().toString();
        String Status = GetBillStatus();
        String EndWaterDate = tv_EndWaterDate.getText().toString();
        String WaterResult = tv_WaterResult.getText().toString();
        String SeeperSource = tv_SeeperSource.getText().toString();
        String WarningID = MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetReasonList(), 0, tv_WarningID.getText().toString());
        String Measures = tv_Measures.getText().toString();
        String SeeperLevel = MyTextUtils.GetOpitemValue(OptionsItemsUtils.GetWaterPointSeeperLevel(), 0, tv_SeeperLevel.getText().toString());
        ls_DisposingPersonName = tv_DisposingPerson.getText().toString();

        String FileStatus = GetFileStatus();
        if (MyTextUtils.IsNullByString(WarningID)) {
            ToastShow("所属预警不能为空");
            return;
        }
        if (MyTextUtils.IsNullByString(Location)) {
            ToastShow("地点不能为空");
            return;
        }

        if (MyTextUtils.IsNullByString(SeeperReason)) {
            ToastShow("积水原因不能为空");
            return;
        }

        try {
            JSONObject maps = new JSONObject();
            if (CacheData.WaterPoint_UPDATE) {//修改
                maps.put("ID", model.getID());

            } else { //新增
                maps.put("CreateDT", MyTextUtils.getNowDatatoString());
                maps.put("Creator", CacheData.UserID());//上报人编号
                maps.put("IsDelete", "S");
            }

            maps.put("SeepeNo", SeepeNo);
            maps.put("Location", Location);
            maps.put("SeeperDepth", SeeperDepth);
            maps.put("SeeperArea", SeeperArea);
            maps.put("Memo", Memo);
            maps.put("StartWaterDate", MyTextUtils.IsNullByString(StartWaterDate) ? null : StartWaterDate);
            maps.put("SeeperReason", SeeperReason);
            maps.put("Status", Status);
            maps.put("DisposingPersonName", ls_DisposingPersonName);//处置人名称
            maps.put("DisposingPerson", ls_DisposingPersonID);//处置人编号
            maps.put("EndWaterDate", MyTextUtils.IsNullByString(EndWaterDate) ? null : EndWaterDate);
            maps.put("WaterResult", WaterResult);
            maps.put("SeeperSource", SeeperSource);
            maps.put("WarningID", WarningID);
            maps.put("SeeperLevel", SeeperLevel);
            maps.put("ArrangementID", ls_ArrangementID);
            maps.put("IsHandle", cb_IsHandle.isChecked() ? 1 : 0);
            maps.put("IsRegression", cb_IsRegression.isChecked() ? 1 : 0);
            maps.put("Measures", Measures);
            maps.put("X", SingleTap_X);
            maps.put("Y", SingleTap_Y);


           if(Status.equals("2")) {
                maps.put("Msg", CacheData.UserName() + ":积水点处置完成。");
                maps.put("OperationType", "Submit");
            }

            selectFileStatus(maps, "保存成功", "保存失败！", true, FileStatus);

        } catch (JSONException e) {
            ToastShow("保存失败," + e.toString());
            e.printStackTrace();
        }
    }


    private void MeasuresSubmit() {
        try {
            String ls_Measures = tv_Measures.getText().toString().trim();
            if (MyTextUtils.IsNullByString(ls_Measures)) {
                ls_Measures = "";
            }
            String FileStatus = GetFileStatus();
            JSONObject maps = new JSONObject();
            maps.put("ID", model.getID());
            maps.put("IsHandle", cb_IsHandle.isChecked() ? 1 : 0);
            maps.put("Measures", ls_Measures);
            selectFileStatus(maps, "预处理成功！", "预处理失败！", true, FileStatus);

        } catch (JSONException e) {
            ToastShow("保存失败," + e.toString());
            e.printStackTrace();
        }
    }

    private void ForwardSubmit(String Memo) {
        try {
            String FileStatus = GetFileStatus();
            JSONObject maps = new JSONObject();
            maps.put("ID", model.getID());
            maps.put("Msg", CacheData.UserName()+":"+model.getLocation() + "积水点无法处置,原因:" + Memo+"。");
            maps.put("OperationType", "Forward");
            maps.put("Status", "0");
            maps.put("DisposingPerson", "");
            maps.put("IsBack", 1);
            maps.put("DisposingPersonName", "");
            maps.put("ArrangementID", "");
            selectFileStatus(maps, "上报成功！", "上报控制室失败！", true, FileStatus);
        } catch (JSONException e) {
            ToastShow("保存失败," + e.toString());
            e.printStackTrace();
        }
    }

    private void selectFileStatus(final JSONObject maps, final String SuccessLog, final String ErrorLog, final boolean isExit, String FileStatus) {
        if (GetFilelist().size() > 0) {
            if (FileStatus.equals("")) {
                ToastShow("请选择附件状态！");
                return;
            } else {
                UploadBill(maps, SuccessLog, ErrorLog, isExit, FileStatus);
            }
        } else {
            UploadBill(maps, SuccessLog, ErrorLog, isExit, FileStatus);
        }
    }

    /**
     * ===============================================
     * 创建时间：2019/3/29 11:28
     * 编 写 人：ShenC
     * 方法说明：保存工单和附件的提交方法
     * ================================================
     * 修改内容：      修改时间：      修改人：
     * ===============================================
     *///保存工单和附件的提交方法
    private void UploadBill(JSONObject maps, final String SuccessLog, final String ErrorLog, final boolean isExit, final String FileStatus) {

        try {
            JSONObject param = new JSONObject();
            param.put("info", maps);
            param.put("filename", "WaterPoint");
            param.put("CreateUserId", CacheData.UserID());
            param.put("CreateUserName", CacheData.UserName());
            param.put("FileStatus", FileStatus);
            dialog = new LoadingDialog(WaterPointDetailActivity.this, "保存中");
            dialog.show();
            UploadUtil request = new UploadUtil(
                    Config.host + WebApi.Api_ParamFileUpload,
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            dialog.close();
                            ToastShow(ErrorLog + ",请求超时！");
                            error.printStackTrace();
                        }
                    }, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject josnobject = new JSONObject(response);
                        if (josnobject.getBoolean("Result")) {

                            dialog.close();
                            ToastShow(SuccessLog);
                            if (isExit) {
                                finish();
                                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                            } else {
                                //清空附件
                                vediofilelist.clear();
                                filelist.clear();
                                filelist.add(new FileIndexBean(true));
                                imageFileAdapter.setNewData(filelist);
                                iv_video_close.setVisibility(View.GONE);
                                add_video1.setImageResource(R.mipmap.icon_addpic_unfocused);
                                initPageData();
                            }


                        } else {
                            dialog.close();
                            ToastShow(ErrorLog + josnobject.getString("Msg"));
                        }
                    } catch (JSONException e) {

                        dialog.close();
                        ToastShow(ErrorLog + e.toString());
                        e.printStackTrace();
                    }
                }

            }, "f_file[]", GetFilelist(), param);
            request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
            mQueue.add(request);
        } catch (JSONException e) {
            dialog.close();
            ToastShow(ErrorLog + e.toString());
            e.printStackTrace();
        }
    }

    //设置工单的状态
    private void SetBillStatus(int Status) {
        switch (Status) {
            case 0:
                ck_status_0.setChecked(true);
                ck_status_1.setChecked(false);
                ck_status_2.setChecked(false);
                break;
            case 1:
                ck_status_0.setChecked(false);
                ck_status_1.setChecked(true);
                ck_status_2.setChecked(false);
                break;
            case 2:
                ck_status_0.setChecked(false);
                ck_status_1.setChecked(false);
                ck_status_2.setChecked(true);
                break;
        }
    }

    //获取附件的状态
    private String GetBillStatus() {
        if (ck_status_0.isChecked()) {
            return "0";
        } else if (ck_status_1.isChecked()) {
            return "1";
        } else if (ck_status_2.isChecked()) {
            return "2";
        }
        return "0";
    }

    private void chooseTime(final int position) {
        TimePickerView.Builder builder = new TimePickerView.Builder(WaterPointDetailActivity.this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
                String time = MyTextUtils.date2String(date, simpleDateFormat);
                switch (position) {
                    case 1:

                       if(GetCommonParameters.GreaterThanNowData(time)){
                           ToastShow("积水起时不能超过当前时间！");
                       }else{
                           tv_StartWaterDate.setText(time);
                       }
                        break;
                    case 2:
                        tv_EndWaterDate.setText(time);
                        break;
                }

            }
        });
        builder.setType(new boolean[]{true, true, true, true, true, false});
        TimePickerView pvTime = builder.build();
        pvTime.setDate(Calendar.getInstance());
        pvTime.show();
    }

    private void chooseDeviceCode(final List<ProvinceBean> items, final int pos) {
        OptionsPickerView.Builder builder = new OptionsPickerView.Builder(WaterPointDetailActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String itemName = items.get(options1).getPickerViewText();
                //String itemValue = items.get(options1).getDescription();
                switch (pos) {
                    case 4:
                        tv_DisposingPerson.setText(itemName);
                        break;
                    case 6:
                        tv_WarningID.setText(itemName);
                        break;
                }
            }
        });
        OptionsPickerView<ProvinceBean> pickerView = new OptionsPickerView<>(builder);
        pickerView.setPicker(items);
        pickerView.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 101 && resultCode == 100) {
            ls_DisposingPersonID = data.getStringExtra("ID");
            ls_DisposingPersonName = data.getStringExtra("Name");
            tv_DisposingPerson.setText(ls_DisposingPersonName);
        }
        if (requestCode == 102 && resultCode == 100) {
            String w_name = data.getStringExtra("Name");
            tv_WaterResult.setText(w_name);
        }
        if (requestCode == 103 && resultCode == 100) {
            String w_name = data.getStringExtra("Name");
            tv_SeeperReason.setText(w_name);
        }

        if (requestCode == 104 && resultCode == 100) {
            String w_name = data.getStringExtra("Name");
            tv_Measures.setText(w_name);
        }

        if (requestCode == 105 && resultCode == 100) {
            String w_name = data.getStringExtra("Name");
            tv_Memo.setText(w_name);
        }

        if (requestCode == 106 && resultCode == 100) {
            String w_name = data.getStringExtra("Name");
            ForwardSubmit(w_name);
        }

        if (requestCode == 1 && resultCode == 100) {
            String w_id = data.getStringExtra("ID");
            String w_name = data.getStringExtra("Name");
            ls_ArrangementID = w_id;
            tv_ArrangementID.setText(w_name);
        }

    }


    //退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return super.onKeyDown(keyCode, event);
        } else {
            return false;
        }
    }


}
